<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/31
 * Time: 15:23
 */

namespace App\Rep;


use App\Models\FinishProduct;
use App\Models\Material;
use App\Models\MaterialDrawLog;
use App\Models\MaterialRetreatLog;
use App\Models\Order;
use App\Models\SemiFinishProduct;
use App\Models\WarehouseOrderChild;
use Illuminate\Support\Facades\DB;

class IndexRep
{

    public function getIndexHeaderDataRep($params){
        return [
            'this_year_order_num'=>$this->getThisYearsOrderNumber(),//获取今年订单量
            'pending_acceptance_order_nunber'=>$this->getPendingAcceptanceOrderNunber(),//获取待验收订单（半成品）
            'shipped_order_number'=>$this->getShippedOrderNumber(),//获取已发货订单
            'unfilled_orders_number'=>$this->getUnfilledOrdersNumber(),//获取未发货订单
            'qty_backordered'=>$this->getQtyBackordered(),//获取备货量
            'quantity_of_orders_in_production'=>$this->getQuantityOfOrdersInProduction($params),//获取生产中订单
            'to_be_reviewed'=>$this->getToBeReviewed()
        ];

    }

    /**
     * 获取待审核数据
     */
    public function getToBeReviewed(){
        return Order::where('status','<>',1)->where('is_del',2)->count();
    }

    /**
     * 获取近30天数据
     */
    public function getThirtyDayOrderNumberRep(){
        $xAxis = [];
        for ($i = 30; $i >= 0; $i--) {
            $xAxis[] = date('Y-m-d', strtotime("-$i days"));
        }
        //p($xAxis);
        $yAxis = [];
        foreach($xAxis as $k=>$v){
            $start_time = $v." 00:00:00";
            $end_time = $v." 23:59:59";
            $yAxis[] = Order::whereBetween('order_date',[$start_time,$end_time])->where('status',1)->count();
        }
        return ['code'=>0,'data'=>['xAxis'=>$xAxis,'yAxis'=>$yAxis],'msg'=>'success'];
    }

    /**
     * 年度下单TOP10客户
     */
    public function getTOP10CustomerRep(){
        $data = [];
        $year = date("Y");
        $start_time = $year."-01-01";
        $end_time = $year."-12-31";
        $list = Order::select(DB::raw('count(*) as order_count, customer_id'))
            ->whereBetween('order_date',[$start_time,$end_time])->where('status',1)
            ->where('customer_id','<>',0)
            ->where('is_del',2)
            ->with('customer')
            ->groupBy('customer_id')
            ->orderBy('order_count','desc')
            ->offset(0)->limit(10)
            ->get()->toArray();
        $arr = array_sort($list,'order_count','desc');
        foreach($arr as $k=>$v){
            $data['xAxis'][] = $v['order_count'];
            $data['yAxis'][] = $v['customer']['customer_name'];
        }
        return ['code'=>0,'data'=>$data,'msg'=>'success'];
    }


    /**
     * 年度产品
     */
    public function getAnnualProductOrderNumberRep(){
        $data = [];
        $year = date("Y");
        $start_time = $year."-01-01";
        $end_time = $year."-12-31";
        $list = Order::select(DB::raw('count(*) as order_count, product_id'))
            ->whereBetween('order_date',[$start_time,$end_time])->where('status',1)
            ->where('is_del',2)
            ->with('product')
            ->groupBy('product_id')
            ->get()->toArray();
        $arr = array_sort($list,'order_count','desc');
        foreach($arr as $k=>$v){
            $data['yAxis'][] = $v['order_count'];
            $data['xAxis'][] = $v['product']['product_name'];
        }
        return ['code'=>0,'data'=>$data,'msg'=>'success'];
    }


    /**
     * 近6个月订单量
     */
    public function getNearlySexMonthsOrderNumberRep(){
        $xAxis = [];
        for ($i = 6; $i >= 0; $i--) {
            $xAxis[] = date('Y-m', strtotime("-$i month"));
        }
        //p($xAxis);
        $yAxis = [];
        foreach($xAxis as $k=>$v){
            $start_time = $v."-01 00:00:00";
            $end_time = date("Y-m-t 23:59:59", strtotime($start_time));
            $yAxis[] = Order::whereBetween('order_date',[$start_time,$end_time])->where('status',1)->count();
        }
        return ['code'=>0,'data'=>['xAxis'=>$xAxis,'yAxis'=>$yAxis],'msg'=>'success'];


    }






    /**
     * 获取今年订单量
     */
    public function getThisYearsOrderNumber(){
        $year = date("Y");
        $start_time = $year."-01-01";
        $end_time = $year."-12-31";
        $count = Order::whereBetween('order_date',[$start_time,$end_time])->where('status',1)->count();
        return $count;
    }


    /**
     * 获取待验收订单（半成品）
     */
    public function getPendingAcceptanceOrderNunber(){
        $count = SemiFinishProduct::where('is_del',2)->count();
        return $count;
    }

    /**
     * 获取已发货订单
     */
   public function getShippedOrderNumber(){
        $year = date("Y");
        $start_time = $year."-01-01";
        $end_time = $year."-12-31";
        return FinishProduct::where('logistics_status',3)
            ->where('is_stock_up',2)
            ->whereBetween('warehouse_entry_time',[$start_time,$end_time])
            ->count();
    }

    /**
     * 获取未发货订单
     */
    public function getUnfilledOrdersNumber(){
//        $year = date("Y");
//        $start_time = $year."-01-01";
//        $end_time = $year."-12-31";
        return FinishProduct::where('logistics_status','<>',3)
            ->where('is_stock_up',2)
            //->whereBetween('warehouse_entry_time',[$start_time,$end_time])
            ->count();
    }


    /**
     * 获取备货量
     */
    public function getQtyBackordered(){
//        $year = date("Y");
//        $start_time = $year."-01-01";
//        $end_time = $year."-12-31";
        return FinishProduct::where('is_stock_up',1)
            //->whereBetween('warehouse_entry_time',[$start_time,$end_time])
            ->count();
    }

    /**
     * 获取生产中订单
     */
    public function getQuantityOfOrdersInProduction($params){
        //$params = [];
        return Order::Schedule($params)->where('is_replenishment',2)->count();
    }


    /**
     * 材料统计分析表
     * 库存中各个材料余量及占比
     * @return void
     */
    public function getMaterialStatisticAnalysisRep(){
        $list = Material::where('is_del',2)->get()->toArray();
        $rep = new MaterialRep();
        foreach($list as $k=>$v){
            //入库
            $sum = WarehouseOrderChild::where('material_id',$v['id'])->sum('num');
            //领料
            $draw_sum = MaterialDrawLog::where('material_id',$v['id'])->sum('num');
            //退料
            $retreat_sum = MaterialRetreatLog::where('material_id',$v['id'])->sum('num');
            $stock = $rep->getMaterialStockRep($v['id']);
            $stock_percentage = ($draw_sum-$retreat_sum)/$sum*100;
            $data['yAxis']['sum'][] = $sum;
            $data['yAxis']['use'][] = $draw_sum-$retreat_sum;
            $data['yAxis']['stock_percentage'][] = $stock_percentage;
            $data['xAxis'][] = $v['name'];
        }
        return $data;
    }



}
